// lista dokumentów

unit Unit7;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, db, FileUtil, Forms, Controls, Graphics, Dialogs, DBGrids,
  StdCtrls, ExtCtrls, IBQuery, IBDatabase, ZDataset;

type

  { TForm7 }

  TForm7 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Datasource1: TDatasource;
    DBGrid1: TDBGrid;
    ibq1: TIBQuery;
    ibq2: TIBQuery;
    ibt1: TIBTransaction;
    ibt2: TIBTransaction;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    procedure RefreshTable;
  end;

var
  Form7: TForm7;

implementation

uses
  Unit8, Unit5;

{$R *.lfm}

{ TForm7 }

procedure TForm7.Button4Click(Sender: TObject);
begin
  if ibq1.Transaction.Active then ibq1.Transaction.Commit;
  close;
end;

procedure TForm7.Button5Click(Sender: TObject);
begin
  RefreshTable;
end;

procedure TForm7.FormShow(Sender: TObject);
begin
  RefreshTable;
end;

// dodoanie nowej definicji dokukentu
procedure TForm7.Button1Click(Sender: TObject);
begin
  form8.Edit1.Text:='';
  form8.Edit2.Text:='';
  form8.RadioGroup1.Enabled:=true;
  form8.RadioGroup1.ItemIndex:=0;
  form8.ActiveControl:=form8.Edit1;

  if form8.ShowModal = mrOK then
  begin
    ibq2.SQL.Clear;
    ibq2.Params.Clear;
    ibq2.SQL.Add('insert into defdok (sygnatura,opis,typdokumentu,data_zal) values (:a,:b,:c,:d)');
    ibq2.ParamByName('a').AsString:=form8.Edit1.Text;
    ibq2.ParamByName('b').AsString:=form8.Edit2.Text;
    ibq2.ParamByName('c').AsInteger:=form8.RadioGroup1.ItemIndex+1; // 1-rozchod, 2-przychod
    ibq2.ParamByName('d').AsDateTime:=Now();
    try
      ibq2.Transaction.StartTransaction;
      ibq2.ExecSQL;
      ibq2.Transaction.Commit;
    except
      ibq2.Transaction.Rollback;
      ShowMessage('Nie udalo sie dodac nowej defnicji dokumentu');
    end;
  end;
  RefreshTable;
end;

//edycja defnicji dokumentu
procedure TForm7.Button2Click(Sender: TObject);
var
  idx : integer;
begin
  idx:=ibq1.FieldByName('defdok_idx').AsInteger;
  form8.Edit1.Text:=ibq1.FieldByName('sygnatura').AsString;
  form8.Edit2.Text:=ibq1.FieldByName('opis').AsString;
  // typ dokumentu jest tylko do odczytu, nie moge przecez zmienic rozchodu w przychod bo co ze stanami
  form8.RadioGroup1.ItemIndex:=ibq1.FieldByName('typdokumentu').AsInteger-1;
  form8.RadioGroup1.Enabled:=false;
  form8.ActiveControl:=form8.Edit1;

  if form8.ShowModal = mrOK then
  begin
    ibq2.SQL.Clear;
    ibq2.Params.Clear;
    ibq2.SQL.Add('update defdok set sygnatura = :a, opis = :b where defdok_idx = :c');
    ibq2.ParamByName('a').AsString:=form8.Edit1.Text;
    ibq2.ParamByName('b').AsString:=form8.Edit2.Text;
    ibq2.ParamByName('c').AsInteger:=idx;
    try
      ibq2.Transaction.StartTransaction;
      ibq2.ExecSQL;
      ibq2.Transaction.Commit;
    except
      ibq2.Transaction.Rollback;
      ShowMessage('Nie udalo sie poprawic definicji dokumentu');
    end;
  end;
  RefreshTable;
end;

// kasowanie
procedure TForm7.Button3Click(Sender: TObject);
var
  idx : integer;
begin
  idx:=ibq1.FieldByName('defdok_idx').AsInteger;

  // szukanie czy definicja zostala uzyta w dokumentach
  ibq2.SQL.Clear;
  ibq2.Params.Clear;
  ibq2.SQL.Add('select * from naglowki where defdok_idx = :a');
  ibq2.ParamByName('a').AsInteger:=idx;
  try
    ibq2.Transaction.StartTransaction;
    ibq2.Open;

    if ibq2.RecordCount <> 0 then
    begin
      ShowMessage('Dokument byl juz wczesniej uzyty. Nie mozna skasowac definicji.');
      exit;
    end;

    ibq2.Transaction.Commit;
  except
    ibq2.Transaction.Rollback;
    ShowMessage('Nie moge sprawdzic czy definicja zosta uzyta, przerywam');
    exit;
  end;

  if form5.ShowModal = mrOK then
  begin
    ibq2.SQL.Clear;
    ibq2.Params.Clear;
    ibq2.SQL.Add('delete from defdok where defdok_idx = :a');
    ibq2.ParamByName('a').AsInteger:=idx;
    try
      ibq2.Transaction.StartTransaction;
      ibq2.ExecSQL;
      ibq2.Transaction.Commit;
    except
      ibq2.Transaction.Rollback;
      ShowMessage('Nie udalo sie skasowac definicji dokumentu');
    end;
  end;
  RefreshTable;
end;

procedure TForm7.RefreshTable;
var
  a : integer;
begin
  a:=-1;
  if ibq1.Active then
  begin
    a:=ibq1.RecNo;
    ibq1.Close;
  end;

  ibq1.SQL.Clear;
  ibq1.Params.Clear;
  ibq1.SQL.Add('select case typdokumentu when 1 then ''rozchodowy'' when 2 then ''przychodowy'' else ''inny'' end,sygnatura,opis,data_zal,numer,defdok_idx,typdokumentu from defdok');
  try
    if ibq1.Transaction.Active then ibq1.Transaction.Commit;;
    ibq1.Transaction.StartTransaction;
    ibq1.Open;
    if a<>-1 then ibq1.RecNo:=a;
  except
    ibq1.Transaction.Rollback;
    ShowMessage('Problem z wyswietleniem tabeli DEFDOK');
  end;
end;

end.

